package com.pochogastos.servlets;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Blob;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pochogastos.dao.UsuarioDAO;
import com.pochogastos.model.Usuario;

/**
 * Servlet implementation class LoadImagen
 */
public class LoadImagen extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public LoadImagen() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Usuario u = (Usuario)request.getSession().getAttribute("USUARIO");
			Blob img = UsuarioDAO.getInstance().loadImage(u.getId());
			byte[ ] imgData = null;
			if(img!=null){
				imgData = img.getBytes(1,(int)img.length());
				response.setContentType("image/jpeg");
				OutputStream o = response.getOutputStream();
				o.write(imgData);
				o.flush();
				o.close();
			}else{
				File file = new File(request.getServletContext().getRealPath("/img/user.jpeg"));
				FileInputStream fis = new FileInputStream(file);
				BufferedInputStream bis = new BufferedInputStream(fis);             
		        response.setContentType("image/jpeg");
				OutputStream o = response.getOutputStream();
		        for (int data; (data = bis.read()) > -1;) {
		          o.write(data);
		        }
		        o.flush();
				o.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/error.jsp");
			request.getSession().setAttribute("ERROR", e.getMessage());
			rd.forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Usuario u = (Usuario)request.getSession().getAttribute("USUARIO");
			Blob img = UsuarioDAO.getInstance().loadImage(u.getId());
			byte[ ] imgData = null;
			if(img!=null){
				imgData = img.getBytes(1,(int)img.length());
				response.setContentType("image/jpeg");
				OutputStream o = response.getOutputStream();
				o.write(imgData);
				o.flush();
				o.close();
			}else{
				File file = new File("../img/user.jpeg");
				FileInputStream fis = new FileInputStream(file);
				BufferedInputStream bis = new BufferedInputStream(fis);             
		        response.setContentType("image/jpeg");
		        BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());
		        for (int data; (data = bis.read()) > -1;) {
		          output.write(data);
		        }
			}
		} catch (Exception e) {
			e.printStackTrace();
			RequestDispatcher rd = getServletContext().getRequestDispatcher("/error.jsp");
			request.getSession().setAttribute("ERROR", e.getMessage());
			rd.forward(request, response);
		}
	}

}
